package com.hualan.sfzyproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzyproject.domain.*;
import com.hualan.sfzyproject.mapper.CoursesMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CoursesService
 * @Description TODO
 * @Author libowen
 * @Date 2025年01月03日 18:47
 * @Version 1.0
 */
@Service
public class CoursesService {
    @Autowired
    private CoursesMapper coursesMapper;


    public PageInfo<Courses> selectAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Courses> allCourses =coursesMapper.selectAll();
        PageInfo<Courses> coursesPageInfo = new PageInfo<>(allCourses);
        return coursesPageInfo;
    }

    public int insert(Courses courses) {
        return coursesMapper.insertSelective(courses);
    }

    public int update(Courses courses) {
        return coursesMapper.updateByPrimaryKeySelective(courses);
    }

    public int delete(Long coursesId) {
        return coursesMapper.deleteByPrimaryKey(coursesId);
    }

    public Courses selectBycoursesId(Long coursesId) {
        return coursesMapper.selectById(coursesId);
    }
//    批量删除
    public boolean batchDelete(List<Long> courseIds) {
        if (courseIds == null || courseIds.isEmpty()) {
            return false;
        }
        return coursesMapper.batchDelete(courseIds) > 0;
    }
//    模糊查询
    public PageInfo<Courses> search(String keyword, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Courses> list = coursesMapper.searchCourses(keyword);
        return new PageInfo<>(list);
    }
//热门课程
    public PageInfo<Courses> hotCourses(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Courses> list = coursesMapper.selectAllDescByStudyCount();
        return new PageInfo<>(list);
    }
    //根据综合排序，人气，好评率，价格，价格区间和分类排序查询
    public PageInfo<Courses> selectAllCoursesAndRank(String type, Long typeId, Integer page, Integer pageSize, Integer minPrice, Integer maxPrice) {
        PageHelper.startPage(page, pageSize);
        List<Courses> list = coursesMapper.selectAllCoursesAndRank(type,typeId,minPrice,maxPrice);
        return new PageInfo<>(list);
    }
    //判断是否收藏
    public String checkCourseCollect(Integer userId, Integer courseId) {
        return coursesMapper.checkCourseCollect(userId, courseId);
    }
    //收藏课程
    public boolean addCourseCollect(CourseCollection courseCollection) {
        coursesMapper.updateByCourseId(courseCollection.getCourseId());
        return coursesMapper.addCourseCollect(courseCollection);
    }
//    //课程交流
//    public PageInfo<CourseQuestions> findCommunication(Integer id,Integer page, Integer pageSize) {
//        PageHelper.startPage(page, pageSize);
//        List<CourseQuestions> list = coursesMapper.findCommunication(id);
//        return new PageInfo<>(list);
//    }
    //改变收藏中的课程购买状态
    public Integer buyCourse(CoursePurchaseRecord coursePurchaseRecord){
        coursesMapper.updateState(coursePurchaseRecord);
        coursesMapper.updatePurchaseVolume(coursePurchaseRecord);
        return coursesMapper.buyCourse(coursePurchaseRecord);
    }
    //添加课程评价
    public Boolean addReview(CourseReviews courseReviews) {
        coursesMapper.updateReviewCount(courseReviews);
        return coursesMapper.addReview(courseReviews);
    }
    //查询课程交流问题
    public PageInfo<CommunicationVO> findQuestion(Integer id,Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CommunicationVO> list = coursesMapper.findQuestion(id);
        return new PageInfo<>(list);
    }
    //查看回复
    public PageInfo<CourseCommunication> findReply(Integer questionId, Integer courseId,Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CourseCommunication> list = coursesMapper.findReply(questionId, courseId);
        return new PageInfo<>(list);
    }
    //添加回复
    public Boolean addReply(CourseCommunication courseCommunication) {
        return coursesMapper.addReply(courseCommunication);
    }

}
